﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace 原神曲目生成器
{
    public class ASystem
    {
        static Char[] A = { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U' };
        static Char[] B = { 'A', 'S', 'D', 'F', 'G', 'H', 'J' };
        static Char[] C = { 'Z', 'X', 'C', 'V', 'B', 'N', 'M' };
        //解析成按键代码
        public static string Decode(List<string> lines)
        {
            int index = 0;
            int steap = 0;
            //找初始标记 *(*/*)%d           
            while (true)
            {
                string line = lines[index];
                if (line==null)
                {
                    return "";
                }
                line = line.Trim();
                if (line.IndexOf('*') != 0)
                {
                    index++;
                }
                else
                {
                    //找到数据 识别
                    Regex regex = new Regex(@"([\d]*)/([\d]*)\S(?<steap>[\d]*)");
                    Match match = regex.Match(line);
                    if (match.Success)
                    {
                        steap = int.Parse(match.Groups["steap"].Value);
                        break;
                    }
                }
                if (index == lines.Count)
                {
                    return "";
                }
            }
            string MusicData = (steap*2)+"\r\n";
            try
            {
                //寻找段标记
                while (true)
                {
                    string line = lines[index];
                    if (line == null)
                    {
                        break;
                    }
                    line=line.Trim();
                    if (line.IndexOf("**") != 0)
                    {
                        index++;
                    }
                    else
                    {
                        //找到行数据 识别
                        Regex regex = new Regex(@"..(?<row>[\d]*).(?<count>[\d]*)");
                        Match match = regex.Match(line);
                         if (match.Success)
                        {
                            int row = int.Parse(match.Groups["row"].Value);
                            int count = int.Parse(match.Groups["count"].Value);
                            List<string> Duan = new List<string>();
                            int[] Duanindex = new int[row];
                            for (int i = 0; i < row; i++)
                            {
                                Duan.Add(lines[index + i * 2 + 1]);
                                Duan.Add(lines[index + i * 2 + 2]);
                                Duanindex[i] = 0;
                            }
                            int length = Duan[1].Length;
                            int bucong = 16 * count / length - 1;
                            //每一位的处理
                            for (int i = 0; i < length; i++)
                            {
                                string bitStr = "";
                                for (int j = 0; j < row; j++)
                                {
                                    char bitdata = Duan[j * 2 + 1].ElementAt(i);
                                    if (bitdata > '0' && bitdata < '8')
                                    {
                                        //是音符
                                        //检查数据是否有问题
                                        if(Duan[j * 2].Length< Duanindex[j])
                                        {
                                            //有问题 无法解析
                                            MessageBox.Show("数据");
                                        }
                                        //判断什么音
                                        char bityinggao = Duan[j * 2].ElementAt(Duanindex[j]);
                                        Duanindex[j]++;
                                        switch (bityinggao)
                                        {
                                            case '+':
                                                bitStr += A[bitdata - '1'];
                                                break;
                                            case '-':
                                                bitStr += C[bitdata - '1'];
                                                break;
                                            default:
                                                bitStr += B[bitdata - '1'];
                                                break;
                                        }
                                    }
                                }
                                if (bitStr == "")
                                {
                                    MusicData += "-";
                                    for (int t = 0; t < bucong; t++)
                                    {
                                        MusicData += "-";
                                    }
                                }
                                else if (bitStr.Length == 1)
                                {
                                    MusicData += bitStr;
                                    for (int t = 0; t < bucong; t++)
                                    {
                                        MusicData += "-";
                                    }
                                }
                                else
                                {
                                    MusicData += ("(" + bitStr + ")");
                                    for (int t = 0; t < bucong; t++)
                                    {
                                        MusicData += "-";
                                    }
                                }
                                if ((i + 1) % (length / count) == 0)
                                {
                                    MusicData += "\r\n";
                                }
                            }
                            //进入下一段
                            index += row * 2;
                        }

                    }
                    if (index == lines.Count)
                    {
                        return MusicData;
                    }
                }
                return MusicData;
            }
            catch (Exception ex)
            {
                return "";
            }
           

        }

    }
   public class music
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
}
